package com.atguigu.guliguli.product.service.impl;

import com.atguigu.guliguli.product.entity.BrandEntity;
import com.atguigu.guliguli.product.entity.CategoryBrandRelationEntity;
import com.atguigu.guliguli.product.entity.CategoryEntity;
import com.atguigu.guliguli.product.mapper.BrandMapper;
import com.atguigu.guliguli.product.mapper.CategoryBrandRelationMapper;
import com.atguigu.guliguli.product.mapper.CategoryMapper;
import com.atguigu.guliguli.product.service.BrandService;
import com.atguigu.guliguli.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 叶子
 * @date: 2021/11/27 10:36
 **/
@Service
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper,CategoryBrandRelationEntity> implements CategoryBrandRelationService {
   @Autowired
   private BrandMapper brandMapper;
   @Autowired
   private CategoryMapper categoryMapper;
   @Autowired
   private CategoryBrandRelationMapper categoryBrandRelationMapper;
   @Autowired
   BrandService brandService;
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catalogId = categoryBrandRelation.getCatalogId();
//        查询详细名字
        BrandEntity brandEntity = brandMapper.selectById(brandId);
        CategoryEntity categoryEntity = categoryMapper.selectById(catalogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatalogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> catalogId = categoryBrandRelationMapper
                .selectList(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                        .eq(CategoryBrandRelationEntity::getCatalogId,catId));
        return catalogId.stream().map(item->{
            Long brandId = item.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }
}